package com.gitee.sunstar.skbase

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView

abstract class BaseBindAdapter<DB:ViewDataBinding,T>(
    private val dataList: List<T>,
    private val viewId: Int
) :
    RecyclerView.Adapter<BaseBindAdapter.BaseVH<DB>>() {

    private var itemClickListener:OnItemClickListener?=null

    override fun onBindViewHolder(holder: BaseVH<DB>, position: Int) {
       holder.itemView.setOnClickListener {
           itemClickListener?.itemClick(position)
       }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseVH<DB> {
        val db = DataBindingUtil.inflate<DB>(LayoutInflater.from(parent.context),viewId,parent,false)
        val holder = BaseVH<DB>(db.root)
        holder.bindingView = db
        return  holder
    }

    override fun getItemCount(): Int = dataList.size


    fun setOnItemClickListener(itemClickListener: OnItemClickListener){
        this.itemClickListener = itemClickListener
    }
    class BaseVH<DB:ViewDataBinding>(itemView: View) : RecyclerView.ViewHolder(itemView){
        var bindingView:DB? = null
    }
    interface OnItemClickListener{
        fun itemClick(index:Int)
    }
}